package com.lykj.modules.shop.controller;

import com.alibaba.fastjson.JSON;
import com.lykj.core.excel.util.ExcelUtil;
import com.lykj.core.secure.LykjUser;
import com.lykj.core.secure.utils.AuthUtil;
import com.lykj.core.tool.utils.DateUtil;
import com.lykj.modules.shop.entity.UserBill;
import com.lykj.modules.shop.excel.UserBillExcel;
import com.lykj.modules.shop.excel.UserExtractExcel;
import com.lykj.modules.shop.service.UserBillService;
import com.lykj.modules.shop.vo.UserBillVO;
import com.lykj.modules.shop.vo.UserBonusVO;
import com.lykj.modules.system.entity.User;
import com.lykj.modules.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.lykj.core.mp.support.Condition;
import com.lykj.core.mp.support.Query;
import com.lykj.core.tool.api.R;
import com.lykj.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lykj.modules.shop.entity.UserExtract;
import com.lykj.modules.shop.vo.UserExtractVO;
import com.lykj.modules.shop.wrapper.UserExtractWrapper;
import com.lykj.modules.shop.service.UserExtractService;
import com.lykj.core.boot.ctrl.LykjController;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 提现表 控制器
 *
 * @author cxl
 * @since 2022-01-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("lykj-shop/user/extract")
@Api(value = "提现表", tags = "提现表接口")
public class UserExtractController extends LykjController {

	private final UserExtractService userExtractService;
	private final UserService userService;
	private final UserBillService userBillService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入userExtract")
	public R<UserExtractVO> detail(UserExtract userExtract) {
		UserExtract detail = userExtractService.getOne(Condition.getQueryWrapper(userExtract));
		return R.data(UserExtractWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 提现表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入userExtract")
	public R<IPage<UserExtractVO>> list(@ApiIgnore @RequestParam Map<String, Object> map, Query query) {//list(UserExtractVO userExtract, Query query) {
		if(query.getAscs()==null && query.getDescs()==null) {
			query.setDescs("id");
		}
		UserExtractVO userExtract = JSON.parseObject(JSON.toJSONString(map), UserExtractVO.class);
//		IPage<UserExtract> pages = userExtractService.page(Condition.getPage(query), Condition.getQueryWrapper(userExtract));
		IPage<UserExtractVO> pages = userExtractService.selectUserExtractPage(Condition.getPage(query), userExtract);
		return R.data(pages);
	}


	/**
	 * 自定义分页 提现表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入userExtract")
	public R<IPage<UserExtractVO>> page(UserExtractVO userExtract, Query query) {
		userExtract.setUserId(AuthUtil.getUserId());
		if(query.getAscs()==null && query.getDescs()==null) {
			query.setDescs("id");
		}
		IPage<UserExtractVO> pages = userExtractService.selectUserExtractPage(Condition.getPage(query), userExtract);
		return R.data(pages);
	}

	/**
	 * 新增 提现表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入userExtract")
	public R save(@Valid @RequestBody UserExtract userExtract) {
		return R.status(userExtractService.save(userExtract));
	}

	/**
	 * 修改 提现表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入userExtract")
	public R update(@Valid @RequestBody UserExtract userExtract) {
		return R.status(userExtractService.updateById(userExtract));
	}

	/**
	 * 新增或修改 提现表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入userExtract")
	public R submit(@Valid @RequestBody UserExtract userExtract) {
		return R.status(userExtractService.saveOrUpdate(userExtract));
	}


	/**
	 * 删除 提现表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userExtractService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 通过 提现表
	 */
	@PostMapping("/agree")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "通过", notes = "传入ids")
	public R agree(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> list = Func.toLongList(ids);
		for(int i=0;i<list.size();i++){
			UserExtract userExtract = new UserExtract();
			userExtract.setId(list.get(i));
			userExtract.setStatus(1);
			userExtractService.updateById(userExtract);
		}
		return R.status(true);
	}

	/**
	 * 拒绝 提现表
	 */
	@PostMapping("/refuse")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "拒绝", notes = "传入ids")
	public R refuse(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> list = Func.toLongList(ids);
		for(int i=0;i<list.size();i++){
			//修改状态
			UserExtract userExtract = userExtractService.getById(list.get(i));//new UserExtract();
			if(userExtract.getStatus()!=0){
				continue;
			}
			userExtract.setStatus(-1);
			userExtractService.updateById(userExtract);
			BigDecimal amount = userExtract.getAmount().add(userExtract.getService());
			//回退金额
			User user = userService.getById(userExtract.getUserId());
			user.setSysBalance(user.getSysBalance().add(amount));
			userService.updateById(user);
			//增加交易记录
			UserBill userBill = new UserBill();
			userBill.setTenantId(user.getTenantId());
			userBill.setUserId(user.getId());
			userBill.setOtherUserId(0L);
			userBill.setLinkId(null);
			userBill.setPm(1);//0转出1转入
			userBill.setTitle("平台余额提现退款");
			userBill.setCategory(0);//0现金1积分2贡献值
			userBill.setType(5);//0增加1减少2奖励3兑换4充值5提现6购物7转账8红包9收益
			userBill.setNumber(userExtract.getAmount());
			userBill.setService(userExtract.getService());
			userBill.setActual(userBill.getNumber().add(userBill.getService()));
			userBill.setBalance(user.getSysBalance());
			userBill.setRemark("平台余额提现退款,金额[" + userBill.getNumber() + "],手续费[" + userBill.getService() + "],实际金额[" + userBill.getActual() + "],余额[" + userBill.getBalance() + "]");
			userBillService.save(userBill);
		}
		return R.status(true);
	}

	/**
	 * 导出提现申请
	 */
	@GetMapping("export")
	@ApiOperationSupport(order = 15)
	@ApiOperation(value = "导出提现申请", notes = "传入userExtract")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		UserExtractVO userExtract = JSON.parseObject(JSON.toJSONString(params), UserExtractVO.class);
		if(Func.isNotEmpty(params.get("queryDate")) && !"undefined".equals(params.get("queryDate"))) {
			String[] date = params.get("queryDate").toString().split(",");
			userExtract.setStartDate(date[0]);
			userExtract.setEndDate(date[1]);
		}
		List<UserExtractExcel> listExcel = userExtractService.export(userExtract);
		ExcelUtil.export(response, "用户申请数据" + DateUtil.time(), "用户申请数据", listExcel, UserExtractExcel.class);
	}
}
